package cn.ondraw.ragflow.controller;

import cn.ondraw.ragflow.pojo.request.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.common.core.domain.R;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.service.ChunkService;
import org.springframework.web.bind.annotation.*;

/**
 * 知识块
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/v1/datasets")
public class ChunkController {
    private final ChunkService chunkService;


    /**
     * 添加块
     *
     * @param datasetId  关联的数据集 ID
     * @param documentId 关联的文档 ID
     * @param request    添加块的请求对象
     * @return 添加块的响应对象
     */
    @PostMapping("/{datasetId}/documents/{documentId}/chunks")
    public R<Object> addChunk(@PathVariable String datasetId, @PathVariable String documentId, @RequestBody AddChunkRequest request) {
        try {
            return R.ok(chunkService.addChunk(datasetId, documentId, request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 列出块
     *
     * @param request 列出块的请求对象
     * @return 列出块的响应对象
     */
    @GetMapping("/{datasetId}/documents/{documentId}/chunks")
    public R<Object> listChunks(ListChunksRequest request, @PathVariable String datasetId, @PathVariable String documentId) {
        try {
            request.setDataset_id(datasetId);
            request.setDocument_id(documentId);
            return R.ok(chunkService.listChunks(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除块
     *
     * @param datasetId  关联的数据集 ID
     * @param documentId 关联的文档 ID
     * @param request    删除块的请求对象
     * @return 删除块的响应对象
     */
    @DeleteMapping("/{datasetId}/documents/{documentId}/chunks")
    public R<Object> deleteChunks(@PathVariable String datasetId, @PathVariable String documentId, @RequestBody DeleteChunksRequest request) {
        try {
            return R.ok(chunkService.deleteChunks(datasetId, documentId, request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新块
     *
     * @param datasetId  关联的数据集 ID
     * @param documentId 关联的文档 ID
     * @param chunkId    要更新的块的 ID
     * @param request    更新块的请求对象
     * @return 更新块的响应对象
     */
    @PutMapping("/{datasetId}/documents/{documentId}/chunks/{chunkId}")
    public R<Object> updateChunk(@PathVariable String datasetId, @PathVariable String documentId, @PathVariable String chunkId, @RequestBody UpdateChunkRequest request) {
        try {
            return R.ok(chunkService.updateChunk(datasetId, documentId, chunkId, request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 检索块
     *
     * @param request 检索块的请求对象
     * @return 检索块的响应对象
     */
    @PostMapping("/retrieval")
    public R<Object> retrieveChunks(@RequestBody RetrieveChunksRequest request) {
        try {
            return R.ok(chunkService.retrieveChunks(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
